package com.hzj.controller;


import com.hzj.pojo.VoteOption;
import com.hzj.pojo.VoteSubject;
import com.hzj.service.VoteOptionService;
import com.hzj.service.VoteSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hzj
 * @since 2021-03-19
 */
@Controller
public class VoteOptionController {
    @Autowired
    VoteOptionService vos;
    @Autowired
    VoteSubjectService vss;

    @GetMapping("/checkNumTicket")
    public String checkNumTicket(int vsid, Model model) {
        List<Map<String, String>> checkNumTicket = vos.checkNumTicket(vsid);
        Map<String, Object> stringObjectMapt = vss.checkSubject(vsid);
        System.out.println("checkNumTicket=========>" + checkNumTicket);
        System.out.println("stringObjectMapt=========>" + stringObjectMapt);
        if (Integer.valueOf(String.valueOf(stringObjectMapt.get("countVi"))) == 0) {
            stringObjectMapt.put("countVi", 1);
        }
        model.addAttribute("checkNumTicket", checkNumTicket);
        model.addAttribute("stringObjectMapt", stringObjectMapt);
        return "voteview";
    }

    @GetMapping("/admin/checkUpdateSubject")
    public String checkUpdateSubject(int vsid, Model model) {
        List<Map<String, String>> checkUpdateSubject = vos.checkNumTicket(vsid);
        Map<String, Object> stringVoMap = vss.checkSubject(vsid);
        model.addAttribute("checkUpdateSubject", checkUpdateSubject);
        model.addAttribute("stringVoMap", stringVoMap);
        model.addAttribute("countMapSubject", checkUpdateSubject.size());
        return "update";
    }

    @PostMapping("/admin/updateSubjectAll")
    public String updateSubjectAll(int countMapSubject, VoteSubject voteSubject, String[] options, String[] optionsDate, Model model) {
//        System.out.println(voteSubject);
        boolean b1 = vss.updateById(voteSubject);
        if (!b1) {
            model.addAttribute("errorUpdate", "修改失败，请重试");
            return "forward:/showList?updateKey=" + 1;
        }
        boolean updateos = vos.updateOptions(options, voteSubject.getVsId());
        if (!updateos) {
            model.addAttribute("errorUpdate", "修改失败，请重试");
            return "forward:/showList?updateKey=" + 1;
        }
        if (countMapSubject != options.length) {
            boolean b = vos.delectOptions(options, Math.toIntExact(voteSubject.getVsId()));
            if (!b) {
                model.addAttribute("errorUpdate", "修改失败，请重试");
                return "forward:/showList?updateKey=" + 1;
            }
        }
        if (optionsDate != null) {
            if (optionsDate.length > 0) {
                boolean b = vos.addOptions(voteSubject.getVsId(), options.length, optionsDate);
                if (!b) {
                    model.addAttribute("errorUpdate", "修改失败，请重试");
                    return "forward:/showList?updateKey=" + 1;
                }
            }
        }

        return "forward:/showList?updateKey=" + 1;
    }
}

